﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
//using HYElectricSystemSetting;

namespace HYRElectric.PowerSystem
{
	public partial class DlgElectricSysManage : System.Windows.Forms.Form
	{
		public Result Result { get; set; }

		public DlgElectricSysManage(Document m_doc)
		{
			this.InitializeComponent();
			this.doc = m_doc;
			this.FillPanelTree();
			this.InitParameters();
			this.GetAllPanelAndPowerValue();
		}

		private void DlgElectricSysManage_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (this.Result == null)
			{
				for (int i = 0; i <= this.rowsCount; i++)
				{
					this.dgvSystemManage.Rows.Add(new object[]
					{
						i.ToString()
					});
					for (int j = 0; j < this.dgvSystemManage.ColumnCount; j++)
					{
						this.dgvSystemManage.Rows[i].Cells[j].Value = "";
					}
				}
				for (int k = 0; k < this.dgvSystemManage.ColumnCount; k++)
				{
					this.widths.Add(this.dgvSystemManage.Columns[k].Width);
				}
			}
		}

		private void dgvSystemManage_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			try
			{
				using (Brush brush = new SolidBrush(this.dgvSystemManage.GridColor))
				{
					using (Brush brush2 = new SolidBrush(e.CellStyle.BackColor))
					{
						using (Pen pen = new Pen(brush))
						{
							e.Graphics.FillRectangle(brush2, e.CellBounds);
							if ((e.RowIndex == 0 || e.RowIndex == 2) && e.ColumnIndex == 0 && e.Value != null)
							{
								e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, Brushes.Black, (float)(e.CellBounds.X + 2), (float)(e.CellBounds.Y + 2), StringFormat.GenericDefault);
							}
							if (e.RowIndex != 0 && e.RowIndex != 2)
							{
								e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
								if (e.Value != null)
								{
									e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, Brushes.Black, (float)(e.CellBounds.X + 2), (float)(e.CellBounds.Y + 2), StringFormat.GenericDefault);
								}
							}
							else
							{
								e.Handled = true;
							}
							e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
						}
					}
				}
				if (e.RowIndex == 0)
				{
					this.dgvSystemManage.Rows[0].Cells[0].Value = "配电箱";
					e.Handled = true;
				}
				if (e.RowIndex == 2)
				{
					this.dgvSystemManage.Rows[2].Cells[0].Value = "回路";
					e.Handled = true;
				}
				this.dgvSystemManage.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
			}
			catch (Exception)
			{
			}
		}

		private void FillPanelTree()
		{
			IEnumerable<Element> enumerable = RevitFilter.GetElementsOfType(this.doc, typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_ElectricalEquipment).ToElements();
			List<Element> list = new List<Element>();
			List<FindPowerAndPanel> allPowerEquipment = this.GetAllPowerEquipment();
			foreach (Element element in enumerable)
			{
				using (List<FindPowerAndPanel>.Enumerator enumerator2 = allPowerEquipment.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (enumerator2.Current.PanelId == element.Id && !list.Contains(element))
						{
							list.Add(element);
						}
					}
				}
			}
			if (list.Count > 0)
			{
				this.tvwPanelManage.ShowLines = true;
				TreeNode treeNode = this.tvwPanelManage.Nodes.Add("配电箱");
				List<PanelTree> list2 = new List<PanelTree>();
				foreach (Element element2 in list)
				{
					if (string.IsNullOrEmpty((element2 as FamilyInstance).get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_SUPPLY_FROM_PARAM).AsString()))
					{
						TreeNode treeNode2 = new TreeNode();
						treeNode2.Tag = element2;
						treeNode2.Text = element2.Name;
						treeNode.Nodes.Add(treeNode2);
						list2.Add(new PanelTree
						{
							Panel = element2,
							Node = treeNode2
						});
					}
				}
				List<Element> list3 = new List<Element>();
				foreach (Element item in list)
				{
					list3.Add(item);
				}
				if (list2.Count > 0)
				{
					foreach (Element element3 in list)
					{
						foreach (PanelTree panelTree in list2)
						{
							if (element3.Id == panelTree.Panel.Id)
							{
								list3.Remove(element3);
							}
						}
					}
					if (list3.Count > 0)
					{
						this.FillTree(list2, list3);
					}
				}
				this.tvwPanelManage.ExpandAll();
				return;
			}
			MessageBox.Show("项目中未检测到电气系统,请检查电气系统是否指定了配电箱。", "盈建科提示");
			this.Result = Result.Failed;
		}

		private void FillTree(List<PanelTree> rootNodes, List<Element> panelList)
		{
			List<PanelTree> list = new List<PanelTree>();
			foreach (PanelTree panelTree in rootNodes)
			{
				foreach (Element element in panelList)
				{
					FamilyInstance familyInstance = element as FamilyInstance;
					if (!string.IsNullOrEmpty(familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_SUPPLY_FROM_PARAM).AsString()))
					{
						ElectricalSystemSet electricalSystems = familyInstance.MEPModel.ElectricalSystems;
						FamilyInstance familyInstance2 = null;
						if (electricalSystems != null)
						{
							foreach (object obj in electricalSystems)
							{
								ElectricalSystem electricalSystem = (ElectricalSystem)obj;
								if (electricalSystem.BaseEquipment.Id == panelTree.Panel.Id)
								{
									familyInstance2 = electricalSystem.BaseEquipment;
									break;
								}
							}
						}
						if (familyInstance2 != null && familyInstance2.Id == panelTree.Panel.Id)
						{
							TreeNode treeNode = new TreeNode();
							treeNode.Tag = element;
							treeNode.Text = element.Name;
							panelTree.Node.Nodes.Add(treeNode);
							list.Add(new PanelTree
							{
								Panel = element,
								Node = treeNode
							});
						}
					}
				}
			}
			List<Element> list2 = new List<Element>();
			foreach (Element item in panelList)
			{
				list2.Add(item);
			}
			if (list.Count > 0)
			{
				foreach (Element element2 in panelList)
				{
					foreach (PanelTree panelTree2 in list)
					{
						if (element2.Id == panelTree2.Panel.Id)
						{
							list2.Remove(element2);
						}
					}
				}
				if (list2.Count > 0)
				{
					this.FillTree(list, list2);
				}
			}
		}

		private void treSystemManage_Leave(object sender, EventArgs e)
		{
			if (this.tvwPanelManage.SelectedNode != null)
			{
				this.tvwPanelManage.SelectedNode.BackColor = System.Drawing.Color.RoyalBlue;
				this.tvwPanelManage.SelectedNode.ForeColor = System.Drawing.Color.White;
			}
		}

		private void treSystemManage_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwPanelManage.SelectedNode != null)
			{
				this.tvwPanelManage.SelectedNode.BackColor = System.Drawing.Color.Empty;
				this.tvwPanelManage.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void InitParameters()
		{
			this.vecLayingMode.Add("WE");
			this.vecLayingMode.Add("DB");
			this.vecLayingMode.Add("TC");
			this.vecLayingMode.Add("BC");
			this.vecLayingMode.Add("CLC");
			this.vecLayingMode.Add("WC");
			this.vecLayingMode.Add("CE");
			this.vecLayingMode.Add("CC");
			this.vecLayingMode.Add("SCE");
			this.vecLayingMode.Add("SR");
			this.vecLayingMode.Add("BE");
			foreach (string item in this.vecLayingMode)
			{
				this.m_strLaying.Items.Add(item);
			}
			this.m_strLaying.ReadOnly = true;
            //this.vecPipeType = this.data.GetConduitType();
			foreach (string item2 in this.vecPipeType)
			{
				this.m_strPipeType.Items.Add(item2);
			}
			this.m_strPipeType.ReadOnly = true;
			if (CStandardManual.GetAllSwitchLocalName(ref this.vecLocalName))
			{
				foreach (string item3 in this.vecLocalName)
				{
					this.m_strSwitchName.Items.Add(item3);
				}
			}
			this.m_strSwitchName.ReadOnly = true;
			if (CStandardManual.GetAllSwitchType(ref this.vecSwitchType))
			{
				foreach (string item4 in this.vecSwitchType)
				{
					this.m_strSwitchType.Items.Add(item4);
				}
			}
			this.m_strSwitchType.ReadOnly = true;
            //this.vecWireType = this.data.GetWireType();
			foreach (string item5 in this.vecWireType)
			{
				this.m_strWireType.Items.Add(item5);
			}
			this.m_strWireType.ReadOnly = true;
		}

		private void GetAllPanelAndPowerValue()
		{
			ICollection<Element> collection = RevitFilter.GetElementsOfType(this.doc, typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_ElectricalEquipment).ToElements();
			if (collection.Count > 0)
			{
				List<FindPowerAndPanel> allPowerEquipment = this.GetAllPowerEquipment();
				foreach (Element element in collection)
				{
					List<ElementId> list = new List<ElementId>();
					bool flag = false;
					foreach (FindPowerAndPanel findPowerAndPanel in allPowerEquipment)
					{
						if (findPowerAndPanel.PanelId == element.Id)
						{
							flag = true;
							if (!list.Contains(findPowerAndPanel.PowerId))
							{
								list.Add(findPowerAndPanel.PowerId);
							}
						}
					}
					if (this.powerList.Count > 0)
					{
						this.powerList.Clear();
					}
					if (list.Count > 0)
					{
						try
						{
							foreach (ElementId power in list)
							{
								this.GetDataOfPower(element, power);
							}
						}
						catch (Exception)
						{
							MessageBox.Show("获取回路参数信息失败!", "盈建科提示");
							this.Result = Result.Failed;
							break;
						}
					}
					if (flag)
					{
						try
						{
							this.GetDataOfPanel(element);
						}
						catch (Exception)
						{
							MessageBox.Show("获取配电箱参数信息失败!", "盈建科提示");
							this.Result = Result.Failed;
							break;
						}
					}
				}
			}
		}

		private Wire GetPanelWire(FamilyInstance panel)
		{
			Wire wire = null;
			ConnectorSet allRefs = RevitUtils.GetDeviceConnector(panel).AllRefs;
			string text = panel.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_SUPPLY_FROM_PARAM).AsString();
			if (string.IsNullOrEmpty(text))
			{
				wire = DlgElectricSysManage.GetPanelWire(wire, panel);
			}
			else
			{
				foreach (object obj in allRefs)
				{
					Element owner = ((Connector)obj).Owner;
					if (owner is Wire)
					{
						Wire wire2 = owner as Wire;
						Element element;
						Element element2;
						RevitUtils.GetWireConnectedEquipment(wire2, out element, out element2);
						if (element != null && element.Id.IntegerValue != panel.Id.IntegerValue && element is FamilyInstance && element.Name == text)
						{
							wire = wire2;
						}
						if (element2 != null && element2.Id.IntegerValue != panel.Id.IntegerValue && element2 is FamilyInstance && element2.Name == text)
						{
							wire = wire2;
						}
					}
				}
			}
			if (wire == null)
			{
				ElectricalSystem electricalSystem = null;
				foreach (object obj2 in panel.MEPModel.ElectricalSystems)
				{
					ElectricalSystem electricalSystem2 = (ElectricalSystem)obj2;
					if (electricalSystem2.BaseEquipment.Id.IntegerValue == panel.Id.IntegerValue)
					{
						electricalSystem = electricalSystem2;
						break;
					}
				}
				ElementSet elements = electricalSystem.Elements;
				bool flag = false;
				foreach (object obj3 in elements)
				{
					foreach (object obj4 in RevitUtils.GetDeviceConnector((Element)obj3).AllRefs)
					{
						Element owner2 = ((Connector)obj4).Owner;
						if (owner2 is Wire)
						{
							wire = (owner2 as Wire);
							flag = true;
							break;
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
			return wire;
		}

		private Wire GetPowerWire(ElectricalSystem electricalSystem)
		{
			Wire wire = null;
			FamilyInstance baseEquipment = electricalSystem.BaseEquipment;
			wire = DlgElectricSysManage.GetPanelWire(wire, baseEquipment);
			if (wire == null)
			{
				ElementSet elements = electricalSystem.Elements;
				bool flag = false;
				foreach (object obj in elements)
				{
					foreach (object obj2 in RevitUtils.GetDeviceConnector((Element)obj).AllRefs)
					{
						Element owner = ((Connector)obj2).Owner;
						if (owner is Wire)
						{
							wire = (owner as Wire);
							flag = true;
							break;
						}
					}
					if (flag)
					{
						break;
					}
				}
			}
			return wire;
		}

		private static Wire GetPanelWire(Wire wire, FamilyInstance panel)
		{
			foreach (object obj in RevitUtils.GetDeviceConnector(panel).AllRefs)
			{
				Element owner = ((Connector)obj).Owner;
				if (owner is Wire)
				{
					Wire wire2 = owner as Wire;
					Element element;
					Element element2;
					RevitUtils.GetWireConnectedEquipment(wire2, out element, out element2);
					if (element != null && element.Id.IntegerValue != panel.Id.IntegerValue && element is FamilyInstance)
					{
						string text = element.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PANEL_PARAM).AsString();
						if (!string.IsNullOrEmpty(text) && text == panel.Name)
						{
							wire = wire2;
							break;
						}
					}
					if (element2 != null && element2.Id.IntegerValue != panel.Id.IntegerValue && element2 is FamilyInstance)
					{
						string text2 = element2.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PANEL_PARAM).AsString();
						if (!string.IsNullOrEmpty(text2) && text2 == panel.Name)
						{
							wire = wire2;
							break;
						}
					}
				}
			}
			return wire;
		}

		private void treSystemManage_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (e.Node.Parent == null)
			{
				return;
			}
			if (this.clickCount == 0)
			{
				this.dgvSystemManage.Rows.RemoveAt(this.dgvSystemManage.Rows.Count - 1);
			}
			if (this.clickCount > 0)
			{
				for (int i = 3; i < this.AddRowsCount + this.rowsCount; i++)
				{
					this.dgvSystemManage.Rows.RemoveAt(3);
				}
			}
			TreeNode node = e.Node;
			if (node.Tag != null)
			{
				List<FindPowerAndPanel> allPowerEquipment = this.GetAllPowerEquipment();
				Element element = node.Tag as Element;
				List<ElementId> list = new List<ElementId>();
				bool flag = false;
				foreach (FindPowerAndPanel findPowerAndPanel in allPowerEquipment)
				{
					if (findPowerAndPanel.PanelId == element.Id)
					{
						flag = true;
						if (!list.Contains(findPowerAndPanel.PowerId))
						{
							list.Add(findPowerAndPanel.PowerId);
						}
					}
				}
				if (list.Count > 0)
				{
					this.powerGuid.Clear();
					for (int j = 0; j < list.Count - 1; j++)
					{
						for (int k = 0; k < list.Count - 1 - j; k++)
						{
							if (list[k] > list[k + 1])
							{
								ElementId value = list[k];
								list[k] = list[k + 1];
								list[k + 1] = value;
							}
						}
					}
					this.AddRowsCount = list.Count;
					for (int l = 3; l < this.rowsCount + this.AddRowsCount; l++)
					{
						this.dgvSystemManage.Rows.Add(new object[]
						{
							l.ToString()
						});
						for (int m = 0; m < this.dgvSystemManage.ColumnCount; m++)
						{
							this.dgvSystemManage.Rows[l].Cells[m].Value = "";
						}
					}
					foreach (ElementId power in list)
					{
						this.GetDataOfPower(element, power);
					}
				}
				if (flag)
				{
					this.GetDataOfPanel(element);
					this.clickCount++;
				}
				if (this.dgvSystemManage.Rows.Count > 16)
				{
					this.dgvSystemManage.ScrollBars = ScrollBars.Both;
				}
			}
		}

		private void GetDataOfPower(Element element, ElementId power)
		{
			CSystemDataBlock csystemDataBlock = new CSystemDataBlock();
			FamilyInstance familyInstance = element as FamilyInstance;
			ElectricalSystem electricalSystem = this.doc.GetElementById(power) as ElectricalSystem;
			ElectricSystemCommand.AddShardParameters(this.doc, familyInstance, electricalSystem);
			Wire powerWire = this.GetPowerWire(electricalSystem);
			for (int i = 0; i < this.allPowerAndPanel.Count; i++)
			{
				if (this.allPowerAndPanel[i].StrGUID == electricalSystem.Id.ToString())
				{
					this.powerGuid.Add(electricalSystem.Id.ToString());
					this.SetNotFirstload(this.allPowerAndPanel[i], this.powerGuid.Count + 2);
					return;
				}
			}
			string value = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PREFIX_SEPARATOR).AsString();
			string value2 = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PREFIX).AsString();
			string text = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_NAME).AsString();
			int num = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_NAMING).AsInteger();
			if (num != 0)
			{
				if (num == 2)
				{
					if (string.IsNullOrEmpty(value))
					{
						familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PREFIX_SEPARATOR).Set("-");
					}
				}
			}
			else
			{
				if (string.IsNullOrEmpty(value2))
				{
					familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PREFIX).Set(text);
				}
				if (string.IsNullOrEmpty(value))
				{
					familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PREFIX_SEPARATOR).Set("-");
				}
				familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_NAMING).Set(0);
			}
			this.doc.Regenerate();
			csystemDataBlock.NDataType = 1;
			csystemDataBlock.StrGUID = electricalSystem.Id.ToString();
			csystemDataBlock.StrNum = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_NUMBER).AsString();
			csystemDataBlock.StrLoadType = electricalSystem.get_Parameter(BuiltInParameter.CIRCUIT_LOAD_CLASSIFICATION_PARAM).AsString();
			double num2 = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEA).AsDouble();
			double num3 = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEB).AsDouble();
			double num4 = electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEC).AsDouble();
			if (num2 != 0.0 && num3 == 0.0 && num4 == 0.0)
			{
				csystemDataBlock.StrPhase = "A";
			}
			else if (num3 != 0.0 && num2 == 0.0 && num4 == 0.0)
			{
				csystemDataBlock.StrPhase = "B";
			}
			else if (num4 != 0.0 && num3 == 0.0 && num2 == 0.0)
			{
				csystemDataBlock.StrPhase = "C";
			}
			else
			{
				csystemDataBlock.StrPhase = "3P";
			}
			try
			{
				double num5 = 0.0;
				foreach (object obj in electricalSystem.Elements)
				{
					FamilyInstance familyInstance2 = ((Element)obj) as FamilyInstance;
					double num6 = UnitConvert.CovertFromAPI((DisplayUnitType)84, familyInstance2.Symbol.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD).AsDouble());
					if (num6 != 0.0)
					{
						num5 += num6;
					}
				}
				if (num5 <= 0.0)
				{
					csystemDataBlock.DPe = UnitConvert.CovertFromAPI((DisplayUnitType)84, electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD).AsDouble());
				}
				else
				{
					csystemDataBlock.DPe = num5;
				}
			}
			catch (Exception)
			{
				csystemDataBlock.DPe = UnitConvert.CovertFromAPI((DisplayUnitType)84, electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD).AsDouble());
			}
            csystemDataBlock.DUr = Math.Round(UnitConvert.CovertFromAPI((DisplayUnitType)72, electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_VOLTAGE).AsDouble()));
			string b = electricalSystem.get_Parameter(BuiltInParameter.CIRCUIT_LOAD_CLASSIFICATION_PARAM).AsString();
			foreach (Element element2 in RevitFilter.GetElementsOfType(this.doc, typeof(ElectricalDemandFactorDefinition)).ToList<Element>())
			{
				ElectricalDemandFactorDefinition electricalDemandFactorDefinition = element2 as ElectricalDemandFactorDefinition;
				if (electricalDemandFactorDefinition.Name == b)
				{
					IEnumerator<ElectricalDemandFactorValue> enumerator3 = electricalDemandFactorDefinition.GetValues().GetEnumerator();
					{
						while (enumerator3.MoveNext())
						{
							ElectricalDemandFactorValue electricalDemandFactorValue = enumerator3.Current;
							if (electricalDemandFactorValue.Factor > 0.0)
							{
								csystemDataBlock.DKx = electricalDemandFactorValue.Factor;
								break;
							}
						}
						break;
					}
				}
			}
			if (csystemDataBlock.DKx == 0.0)
			{
				csystemDataBlock.DKx = Math.Round(familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM).AsDouble(), 2);
			}
			csystemDataBlock.DCos = Math.Round(electricalSystem.get_Parameter(BuiltInParameter.RBS_ELEC_POWER_FACTOR).AsDouble(), 2);
			double num7 = csystemDataBlock.DKx * csystemDataBlock.DPe;
			double num8 = num7 * (Math.Sqrt(1.0 - csystemDataBlock.DCos * csystemDataBlock.DCos) / csystemDataBlock.DCos);
			double num9 = Math.Sqrt(num7 * num7 + num8 * num8);
			double dic;
			if (csystemDataBlock.StrPhase == "3P")
			{
				dic = Math.Round(num9 / (Math.Sqrt(3.0) * csystemDataBlock.DUr), 2);
			}
			else
			{
				dic = Math.Round(num9 / csystemDataBlock.DUr, 2);
			}
			csystemDataBlock.DIc = dic;
			bool flag = false;
			csystemDataBlock.StrWireType = electricalSystem.GetParameter("导线类型").AsValueString();
			if (!string.IsNullOrEmpty(csystemDataBlock.StrWireType))
			{
				foreach (object obj2 in this.m_strWireType.Items)
				{
					if (csystemDataBlock.StrWireType == obj2.ToString())
					{
						WireType wireType = null;
						foreach (object obj3 in this.doc.Settings.ElectricalSetting.WireTypes)
						{
							WireType wireType2 = (WireType)obj3;
							if (wireType2.Name.Equals(csystemDataBlock.StrWireType))
							{
								wireType = wireType2;
								break;
							}
						}
						if (wireType != null)
						{
							flag = true;
							break;
						}
						break;
					}
				}
			}
			if (!flag)
			{
				bool flag2 = false;
				if (powerWire != null)
				{
					csystemDataBlock.StrWireType = powerWire.GetParameter("导线类型").AsString();
					if (!string.IsNullOrEmpty(csystemDataBlock.StrWireType))
					{
						foreach (object obj4 in this.m_strWireType.Items)
						{
							if (csystemDataBlock.StrWireType == obj4.ToString())
							{
								flag2 = true;
							}
						}
					}
				}
				if (!flag2)
				{
					csystemDataBlock.StrWireType = this.vecWireType[0];
				}
			}
			csystemDataBlock.NWireNum = 3;
			if (powerWire == null)
			{
				csystemDataBlock.StrLaying = this.vecLayingMode[0];
			}
			else
			{
				csystemDataBlock.StrLaying = powerWire.GetParameter("敷设方式").AsString();
				if (string.IsNullOrEmpty(csystemDataBlock.StrLaying))
				{
					csystemDataBlock.StrLaying = this.vecLayingMode[0];
				}
			}
            List<double> wireSize = new List<double>();// this.data.GetWireSize(csystemDataBlock.StrWireType);
			csystemDataBlock.DWireS = wireSize[0];
			if (powerWire == null)
			{
				csystemDataBlock.StrPipeType = this.vecPipeType[0];
			}
			else
			{
				csystemDataBlock.StrPipeType = powerWire.GetParameter("线管类型").AsString();
				if (string.IsNullOrEmpty(csystemDataBlock.StrPipeType))
				{
					csystemDataBlock.StrPipeType = this.vecPipeType[0];
				}
			}
            double pipeSize = 0;// this.data.GetPipeSize(csystemDataBlock.StrWireType, (int)csystemDataBlock.NWireNum, csystemDataBlock.StrPipeType, csystemDataBlock.DWireS);
			double dpipeDia = this.FindProximal(csystemDataBlock.StrPipeType, pipeSize);
			csystemDataBlock.DPipeDia = dpipeDia;
			csystemDataBlock.StrSwitchName = electricalSystem.GetParameter("开关名称").AsString();
			if (string.IsNullOrEmpty(csystemDataBlock.StrSwitchName))
			{
				csystemDataBlock.StrSwitchName = this.vecLocalName[0];
			}
			csystemDataBlock.StrSwitchType = electricalSystem.GetParameter("开关型号").AsString();
			if (string.IsNullOrEmpty(csystemDataBlock.StrSwitchType))
			{
				csystemDataBlock.StrSwitchType = this.vecSwitchType[0];
			}
			double num10 = 0.0;
			CStandardManual.GetSwitchSpec(csystemDataBlock.StrSwitchType, csystemDataBlock.DIc, ref num10);
			csystemDataBlock.StrSwitchSpec = num10.ToString();
			csystemDataBlock.StrParentGUID = familyInstance.Id.ToString();
			this.powerList.Add(csystemDataBlock);
			bool flag3 = false;
			using (List<CSystemDataBlock>.Enumerator enumerator5 = this.allPowerAndPanel.GetEnumerator())
			{
				while (enumerator5.MoveNext())
				{
					if (enumerator5.Current.StrGUID == csystemDataBlock.StrGUID)
					{
						flag3 = true;
					}
				}
			}
			if (!flag3)
			{
				this.allPowerAndPanel.Add(csystemDataBlock);
			}
		}

		private void GetDataOfPanel(Element element)
		{
			this.panelGuid = "";
			FamilyInstance familyInstance = element as FamilyInstance;
			CSystemDataBlock csystemDataBlock = new CSystemDataBlock();
			Wire panelWire = this.GetPanelWire(familyInstance);
			foreach (CSystemDataBlock csystemDataBlock2 in this.allPowerAndPanel)
			{
				if (csystemDataBlock2.StrGUID == familyInstance.Id.ToString())
				{
					this.SetNotFirstload(csystemDataBlock2, 1);
					this.panelGuid = familyInstance.Id.ToString();
					return;
				}
			}
			csystemDataBlock.NDataType = 0;
			csystemDataBlock.StrGUID = familyInstance.Id.ToString();
			csystemDataBlock.StrNum = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_NAME).AsString();
			csystemDataBlock.StrLoadType = this.doc.GetElementById(familyInstance.GetTypeId()).GetParameter("负荷分类").AsValueString();
			if (this.doc.GetElementById(familyInstance.GetTypeId()).GetParameter("极数").AsInteger() == 1)
			{
				bool flag = false;
				double num = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEA).AsDouble();
				double num2 = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEB).AsDouble();
				double num3 = familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_APPARENT_LOAD_PHASEC).AsDouble();
				if (num != 0.0)
				{
					flag = true;
					csystemDataBlock.StrPhase = "A";
				}
				if (!flag && num2 != 0.0)
				{
					flag = true;
					csystemDataBlock.StrPhase = "B";
				}
				if (!flag && num3 != 0.0)
				{
					flag = true;
					csystemDataBlock.StrPhase = "C";
				}
				if (!flag)
				{
					csystemDataBlock.StrPhase = "A";
				}
			}
			else if (this.doc.GetElementById(familyInstance.GetTypeId()).GetParameter("极数").AsInteger() == 3)
			{
				csystemDataBlock.StrPhase = "3P";
			}
			double num4 = 0.0;
			if (this.powerList.Count > 0)
			{
				for (int i = 0; i < this.powerList.Count; i++)
				{
					num4 += this.powerList[i].DPe;
				}
			}
			csystemDataBlock.DPe = num4;
			double value = (this.doc.GetElementById(familyInstance.GetTypeId()) as FamilySymbol).GetParameter("额定电压").AsDouble();
			csystemDataBlock.DUr = Math.Round(UnitConvert.CovertFromAPI((DisplayUnitType)84, value));
			csystemDataBlock.DKx = Math.Round(familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_PANEL_TOTAL_DEMAND_FACTOR_PARAM).AsDouble(), 2);
			double num7;
			double num8;
			if (csystemDataBlock.StrPhase == "3P")
			{
				List<double> list = new List<double>();
				List<double> list2 = new List<double>();
				List<double> list3 = new List<double>();
				List<double> list4 = new List<double>();
				for (int j = 0; j < this.powerList.Count; j++)
				{
					if (this.powerList[j].StrPhase != "3P")
					{
						double num5 = this.powerList[j].DKx * this.powerList[j].DPe;
						double item = num5 * (Math.Sqrt(1.0 - this.powerList[j].DCos * this.powerList[j].DCos) / this.powerList[j].DCos);
						list.Add(num5);
						list2.Add(item);
					}
					else
					{
						double item2 = this.powerList[j].DKx * this.powerList[j].DPe;
						double num6 = Math.Sqrt(1.0 - this.powerList[j].DCos * this.powerList[j].DCos) / this.powerList[j].DCos;
						list3.Add(item2);
						list4.Add(item2);
					}
				}
				num7 = list.Max() * 3.0 + list3.Sum();
				num8 = list2.Max() * 3.0 + list4.Sum();
				double num9 = Math.Sqrt(num7 * num7 + num8 * num8);
				csystemDataBlock.DIc = Math.Round(num9 / (Math.Sqrt(3.0) * csystemDataBlock.DUr), 2);
			}
			else
			{
				List<double> list5 = new List<double>();
				List<double> list6 = new List<double>();
				for (int k = 0; k < this.powerList.Count; k++)
				{
					if (this.powerList[k].StrPhase != "3P")
					{
						double num10 = this.powerList[k].DKx * this.powerList[k].DPe;
						double item3 = num10 * (Math.Sqrt(1.0 - this.powerList[k].DCos * this.powerList[k].DCos) / this.powerList[k].DCos);
						list5.Add(num10);
						list6.Add(item3);
					}
					else
					{
						double item4 = this.powerList[k].DKx * this.powerList[k].DPe;
						double num11 = Math.Sqrt(1.0 - this.powerList[k].DCos * this.powerList[k].DCos) / this.powerList[k].DCos;
						list5.Add(item4);
						list6.Add(item4);
					}
				}
				num7 = list5.Sum();
				num8 = list6.Sum();
				double num12 = Math.Sqrt(num7 * num7 + num8 * num8);
				csystemDataBlock.DIc = Math.Round(num12 / csystemDataBlock.DUr, 2);
			}
			csystemDataBlock.DCos = Math.Round(Math.Cos(Math.Atan(num8 / num7)), 2);
			bool flag2 = false;
			csystemDataBlock.StrWireType = familyInstance.GetParameter("导线类型").AsString();
			if (!string.IsNullOrEmpty(csystemDataBlock.StrWireType))
			{
				foreach (object obj in this.m_strWireType.Items)
				{
					if (csystemDataBlock.StrWireType == obj.ToString())
					{
						WireType wireType = null;
						foreach (object obj2 in this.doc.Settings.ElectricalSetting.WireTypes)
						{
							WireType wireType2 = (WireType)obj2;
							if (wireType2.Name.Equals(csystemDataBlock.StrWireType))
							{
								wireType = wireType2;
								break;
							}
						}
						if (wireType != null)
						{
							flag2 = true;
							break;
						}
						break;
					}
				}
			}
			if (!flag2)
			{
				bool flag3 = false;
				if (panelWire != null)
				{
					csystemDataBlock.StrWireType = panelWire.GetParameter("导线类型").AsString();
					if (!string.IsNullOrEmpty(csystemDataBlock.StrWireType))
					{
						foreach (object obj3 in this.m_strWireType.Items)
						{
							if (csystemDataBlock.StrWireType == obj3.ToString())
							{
								flag3 = true;
							}
						}
					}
				}
				if (!flag3)
				{
					csystemDataBlock.StrWireType = this.vecWireType[0];
				}
			}
			csystemDataBlock.NWireNum = 3;
			if (panelWire == null)
			{
				csystemDataBlock.StrLaying = this.vecLayingMode[0];
			}
			else
			{
				csystemDataBlock.StrLaying = panelWire.GetParameter("敷设方式").AsString();
				if (string.IsNullOrEmpty(csystemDataBlock.StrLaying))
				{
					csystemDataBlock.StrLaying = this.vecLayingMode[0];
				}
			}
            List<double> wireSize = new List<double>();// this.data.GetWireSize(csystemDataBlock.StrWireType);
			csystemDataBlock.DWireS = wireSize[0];
			if (panelWire == null)
			{
				csystemDataBlock.StrPipeType = this.vecPipeType[0];
			}
			else
			{
				csystemDataBlock.StrPipeType = panelWire.GetParameter("线管类型").AsString();
				if (string.IsNullOrEmpty(csystemDataBlock.StrPipeType))
				{
					csystemDataBlock.StrPipeType = this.vecPipeType[0];
				}
			}
            double pipeSize = 0;// this.data.GetPipeSize(csystemDataBlock.StrWireType, (int)csystemDataBlock.NWireNum, csystemDataBlock.StrPipeType, csystemDataBlock.DWireS);
			double dpipeDia = this.FindProximal(csystemDataBlock.StrPipeType, pipeSize);
			csystemDataBlock.DPipeDia = dpipeDia;
			csystemDataBlock.StrSwitchName = familyInstance.GetParameter("开关名称").AsString();
			if (string.IsNullOrEmpty(csystemDataBlock.StrSwitchName))
			{
				csystemDataBlock.StrSwitchName = this.vecLocalName[0];
			}
			csystemDataBlock.StrSwitchType = familyInstance.GetParameter("开关型号").AsString();
			if (string.IsNullOrEmpty(csystemDataBlock.StrSwitchType))
			{
				csystemDataBlock.StrSwitchType = this.vecSwitchType[0];
			}
			double num13 = 0.0;
			CStandardManual.GetSwitchSpec(csystemDataBlock.StrSwitchType, csystemDataBlock.DIc, ref num13);
			csystemDataBlock.StrSwitchSpec = num13.ToString();
			string strParentGUID = "";
			ElectricalSystemSet electricalSystems = familyInstance.MEPModel.ElectricalSystems;
			if (electricalSystems != null)
			{
				foreach (object obj4 in electricalSystems)
				{
					strParentGUID = ((ElectricalSystem)obj4).Id.ToString();
				}
				csystemDataBlock.StrParentGUID = strParentGUID;
			}
			bool flag4 = false;
			using (List<CSystemDataBlock>.Enumerator enumerator = this.allPowerAndPanel.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.StrGUID == csystemDataBlock.StrGUID)
					{
						flag4 = true;
					}
				}
			}
			if (!flag4)
			{
				this.allPowerAndPanel.Add(csystemDataBlock);
			}
		}

		private List<FindPowerAndPanel> GetAllPowerEquipment()
		{
			List<FindPowerAndPanel> list = new List<FindPowerAndPanel>();
			foreach (Element element in RevitFilter.GetElementsOfType(this.doc, typeof(FamilyInstance)).ToElements())
			{
				FamilyInstance familyInstance = element as FamilyInstance;
				bool flag = false;
				try
				{
					ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
						if ((connectorSetIterator.Current as Connector).Domain == (Autodesk.Revit.DB.Domain)2)
						{
							flag = true;
							break;
						}
					}
				}
				catch (Exception)
				{
					continue;
				}
				if (flag && !string.IsNullOrEmpty(familyInstance.get_Parameter(BuiltInParameter.RBS_ELEC_CIRCUIT_PANEL_PARAM).AsString()))
				{
					ElectricalSystemSet electricalSystems = familyInstance.MEPModel.ElectricalSystems;
					if (electricalSystems != null)
					{
						foreach (object obj in electricalSystems)
						{
							ElectricalSystem electricalSystem = (ElectricalSystem)obj;
							FindPowerAndPanel item = new FindPowerAndPanel
							{
								PanelId = electricalSystem.BaseEquipment.Id,
								PowerId = electricalSystem.Id
							};
							if (!list.Contains(item))
							{
								list.Add(item);
							}
						}
					}
				}
			}
			return list;
		}

		private void dgvSystemManage_CurrentCellChanged(object sender, EventArgs e)
		{
			bool flag = false;
			for (int i = 0; i < this.dgvSystemManage.Rows.Count; i++)
			{
				if ((this.dgvSystemManage.CurrentCell == this.dgvSystemManage.Rows[i].Cells[this.m_strWireType.Name] && this.dgvSystemManage.Rows[i].Cells[this.m_strWireType.Name].Value.ToString() != "") || (this.dgvSystemManage.CurrentCell == this.dgvSystemManage.Rows[i].Cells[this.m_strLaying.Name] && this.dgvSystemManage.Rows[i].Cells[this.m_strLaying.Name].Value.ToString() != "") || (this.dgvSystemManage.CurrentCell == this.dgvSystemManage.Rows[i].Cells[this.m_strPipeType.Name] && this.dgvSystemManage.Rows[i].Cells[this.m_strPipeType.Name].Value.ToString() != "") || (this.dgvSystemManage.CurrentCell == this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchName.Name] && this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchName.Name].Value.ToString() != "") || (this.dgvSystemManage.CurrentCell == this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchType.Name] && this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchType.Name].Value.ToString() != ""))
				{
					flag = true;
				}
			}
			if (flag)
			{
				this.m_strLaying.ReadOnly = false;
				this.m_strPipeType.ReadOnly = false;
				this.m_strSwitchName.ReadOnly = false;
				this.m_strSwitchType.ReadOnly = false;
				this.m_strWireType.ReadOnly = false;
				return;
			}
			this.m_strLaying.ReadOnly = true;
			this.m_strPipeType.ReadOnly = true;
			this.m_strSwitchName.ReadOnly = true;
			this.m_strSwitchType.ReadOnly = true;
			this.m_strWireType.ReadOnly = true;
		}

		private void SetNotFirstload(CSystemDataBlock item, int i)
		{
			this.dgvSystemManage.Rows[i].Cells[this.m_strNum.Name].Value = item.StrNum;
			this.dgvSystemManage.Rows[i].Cells[this.m_strLoadType.Name].Value = item.StrLoadType;
			this.dgvSystemManage.Rows[i].Cells[this.m_strPhase.Name].Value = item.StrPhase;
			this.dgvSystemManage.Rows[i].Cells[this.m_dPe.Name].Value = Math.Round(item.DPe / 1000.0, 2);
			this.dgvSystemManage.Rows[i].Cells[this.m_dUr.Name].Value = item.DUr;
			this.dgvSystemManage.Rows[i].Cells[this.m_nWireNum.Name].Value = item.NWireNum;
			this.dgvSystemManage.Rows[i].Cells[this.m_dKx.Name].Value = item.DKx.ToString();
			this.dgvSystemManage.Rows[i].Cells[this.m_strWireType.Name].Value = item.StrWireType;
			this.dgvSystemManage.Rows[i].Cells[this.m_dCos.Name].Value = item.DCos;
			this.dgvSystemManage.Rows[i].Cells[this.m_dIc.Name].Value = item.DIc;
			this.dgvSystemManage.Rows[i].Cells[this.m_dWireS.Name].Value = item.DWireS;
			this.dgvSystemManage.Rows[i].Cells[this.m_strPipeType.Name].Value = item.StrPipeType;
			this.dgvSystemManage.Rows[i].Cells[this.m_dPipeDia.Name].Value = item.DPipeDia;
			this.dgvSystemManage.Rows[i].Cells[this.m_strLaying.Name].Value = item.StrLaying;
			this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchName.Name].Value = item.StrSwitchName;
			this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchType.Name].Value = item.StrSwitchType;
			this.dgvSystemManage.Rows[i].Cells[this.m_strSwitchSpec.Name].Value = item.StrSwitchSpec;
		}

		private void dgvSystemManage_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (this.dgvSystemManage.CurrentCell.OwningColumn.Name == "m_strWireType" || this.dgvSystemManage.CurrentCell.OwningColumn.Name == "m_strPipeType" || this.dgvSystemManage.CurrentCell.OwningColumn.Name == "m_strSwitchType")
			{
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged -= this.ComboBox_SelectedIndexChanged;
				((System.Windows.Forms.ComboBox)e.Control).SelectedIndexChanged += this.ComboBox_SelectedIndexChanged;
			}
		}

		private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			int rowIndex = this.dgvSystemManage.CurrentCell.RowIndex;
			string name = this.dgvSystemManage.CurrentCell.OwningColumn.Name;
			this.btnModifyValue.Enabled = true;
			string text = (sender as System.Windows.Forms.ComboBox).SelectedItem.ToString();
			if (name == "m_strPipeType")
			{
                double pipeSize = 0;// this.data.GetPipeSize(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_strWireType.Name].Value.ToString(), (int)Convert.ToUInt16(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_nWireNum.Name].Value), text, Convert.ToDouble(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dWireS.Name].Value));
				double num = this.FindProximal(text, pipeSize);
				this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dPipeDia.Name].Value = num;
				return;
			}
			if (name == "m_strSwitchType")
			{
				double num2 = 0.0;
				CStandardManual.GetSwitchSpec(text, Convert.ToDouble(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dIc.Name].Value), ref num2);
				this.dgvSystemManage.Rows[rowIndex].Cells[this.m_strSwitchSpec.Name].Value = num2.ToString();
				return;
			}
			if (name == "m_strWireType")
			{
                List<double> wireSize = new List<double>();// this.data.GetWireSize(text);
				this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dWireS.Name].Value = wireSize[0];
                double pipeSize2 = 0;// this.data.GetPipeSize(text, (int)Convert.ToUInt16(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_nWireNum.Name].Value), this.dgvSystemManage.Rows[rowIndex].Cells[this.m_strPipeType.Name].Value.ToString(), Convert.ToDouble(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dWireS.Name].Value));
				double num3 = this.FindProximal(this.dgvSystemManage.Rows[rowIndex].Cells[this.m_strPipeType.Name].Value.ToString(), pipeSize2);
				this.dgvSystemManage.Rows[rowIndex].Cells[this.m_dPipeDia.Name].Value = num3;
			}
		}

		private double FindProximal(string conduitType, double target)
		{
            //List<double> allWireWearPipe = this.data.GetAllWireWearPipe(conduitType);
			List<double> list = new List<double>();
            //double num = Math.Abs(target - allWireWearPipe[0]);
            //foreach (double num2 in allWireWearPipe)
            //{
            //    double num3 = Math.Abs(target - num2);
            //    if (num3 < num)
            //    {
            //        num = num3;
            //        list.Clear();
            //        list.Add(num2);
            //    }
            //    else if (num3 == num)
            //    {
            //        list.Add(num2);
            //    }
            //}
			return list[0];
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.RecordModifySysValue();
			this.SaveChangeValue();
		}

		private void btnModifyValue_Click(object sender, EventArgs e)
		{
			this.dgvValueIsNull = false;
			this.RecordModifySysValue();
		}

		private void RecordModifySysValue()
		{
			if (this.powerGuid.Count > 0)
			{
				for (int i = 0; i < this.powerGuid.Count; i++)
				{
					for (int j = 0; j < this.allPowerAndPanel.Count; j++)
					{
						if (this.allPowerAndPanel[j].StrGUID == this.powerGuid[i])
						{
							this.allPowerAndPanel[j].DPipeDia = Convert.ToDouble(this.dgvSystemManage.Rows[i + 3].Cells[this.m_dPipeDia.Name].Value);
							this.allPowerAndPanel[j].StrSwitchSpec = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strSwitchSpec.Name].Value.ToString();
							this.allPowerAndPanel[j].StrLaying = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strLaying.Name].Value.ToString();
							this.allPowerAndPanel[j].StrPipeType = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strPipeType.Name].Value.ToString();
							this.allPowerAndPanel[j].StrSwitchName = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strSwitchName.Name].Value.ToString();
							this.allPowerAndPanel[j].StrSwitchType = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strSwitchType.Name].Value.ToString();
							this.allPowerAndPanel[j].StrWireType = this.dgvSystemManage.Rows[i + 3].Cells[this.m_strWireType.Name].Value.ToString();
							this.allPowerAndPanel[j].DWireS = Convert.ToDouble(this.dgvSystemManage.Rows[i + 3].Cells[this.m_dWireS.Name].Value);
						}
					}
				}
			}
			if (this.panelGuid != "")
			{
				for (int k = 0; k < this.allPowerAndPanel.Count; k++)
				{
					if (this.allPowerAndPanel[k].StrGUID == this.panelGuid)
					{
						this.allPowerAndPanel[k].DPipeDia = Convert.ToDouble(this.dgvSystemManage.Rows[1].Cells[this.m_dPipeDia.Name].Value);
						this.allPowerAndPanel[k].StrSwitchSpec = this.dgvSystemManage.Rows[1].Cells[this.m_strSwitchSpec.Name].Value.ToString();
						this.allPowerAndPanel[k].StrLaying = this.dgvSystemManage.Rows[1].Cells[this.m_strLaying.Name].Value.ToString();
						this.allPowerAndPanel[k].StrPipeType = this.dgvSystemManage.Rows[1].Cells[this.m_strPipeType.Name].Value.ToString();
						this.allPowerAndPanel[k].StrSwitchName = this.dgvSystemManage.Rows[1].Cells[this.m_strSwitchName.Name].Value.ToString();
						this.allPowerAndPanel[k].StrSwitchType = this.dgvSystemManage.Rows[1].Cells[this.m_strSwitchType.Name].Value.ToString();
						this.allPowerAndPanel[k].StrWireType = this.dgvSystemManage.Rows[1].Cells[this.m_strWireType.Name].Value.ToString();
						this.allPowerAndPanel[k].DWireS = Convert.ToDouble(this.dgvSystemManage.Rows[1].Cells[this.m_dWireS.Name].Value);
					}
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (!this.dgvValueIsNull)
			{
				this.SaveChangeValue();
			}
		}

		private void SaveChangeValue()
		{
			foreach (CSystemDataBlock csystemDataBlock in this.allPowerAndPanel)
			{
				if (csystemDataBlock.NDataType == 0)
				{
					Element element = null;
					foreach (Element element2 in RevitFilter.GetElementsOfType(this.doc, typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_ElectricalEquipment).ToElements())
					{
						if ((element2 as FamilyInstance).Id.ToString() == csystemDataBlock.StrGUID)
						{
							element = element2;
							break;
						}
					}
					if (element != null)
					{
						FamilyInstance elem = element as FamilyInstance;
						elem.GetParameter("功率因数").Set(csystemDataBlock.DCos);
						elem.GetParameter("导线截面").Set(csystemDataBlock.DWireS.ToString());
						elem.GetParameter("开关名称").Set(csystemDataBlock.StrSwitchName);
						elem.GetParameter("开关型号").Set(csystemDataBlock.StrSwitchType);
						elem.GetParameter("开关规格").Set(csystemDataBlock.StrSwitchSpec);
						elem.GetParameter("敷设方式").Set(csystemDataBlock.StrLaying);
						elem.GetParameter("穿管管径").Set(csystemDataBlock.DPipeDia / 304.8);
						elem.GetParameter("穿管类型").Set(csystemDataBlock.StrPipeType);
						elem.GetParameter("导线类型").Set(csystemDataBlock.StrWireType);
					}
				}
				else
				{
					Element element3 = null;
					foreach (Element element4 in RevitFilter.GetElementsOfType(this.doc, typeof(ElectricalSystem)).OfCategory(BuiltInCategory.OST_ElectricalCircuit).ToElements())
					{
						if ((element4 as ElectricalSystem).Id.ToString() == csystemDataBlock.StrGUID)
						{
							element3 = element4;
							break;
						}
					}
					if (element3 != null)
					{
						element3.GetParameter("导线截面").Set(csystemDataBlock.DWireS.ToString());
						element3.GetParameter("开关名称").Set(csystemDataBlock.StrSwitchName);
						element3.GetParameter("开关型号").Set(csystemDataBlock.StrSwitchType);
						element3.GetParameter("开关规格").Set(csystemDataBlock.StrSwitchSpec);
						element3.GetParameter("敷设方式").Set(csystemDataBlock.StrLaying);
						element3.GetParameter("穿管管径").Set(csystemDataBlock.DPipeDia / 304.8);
						element3.GetParameter("穿管类型").Set(csystemDataBlock.StrPipeType);
						WireType wireType = null;
						foreach (object obj in this.doc.Settings.ElectricalSetting.WireTypes)
						{
							WireType wireType2 = (WireType)obj;
							if (wireType2.Name.Equals(csystemDataBlock.StrWireType))
							{
								wireType = wireType2;
								break;
							}
						}
						if (wireType == null)
						{
                            //wireType = ElectricSystemSettingData.AddWireType(this.doc, csystemDataBlock.StrWireType);
						}
						element3.GetParameter("导线类型").Set(wireType.Id);
					}
				}
			}
		}

		private void dgvSystemManage_SizeChanged(object sender, EventArgs e)
		{
			int columnsWidth = this.dgvSystemManage.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
			int width = this.dgvSystemManage.Width;
			if (width > columnsWidth)
			{
				int nums = width - columnsWidth;
				int[] columnWidthArrs = this.GetColumnWidthArrs(nums, this.dgvSystemManage.ColumnCount);
				for (int i = 0; i < columnWidthArrs.Length; i++)
				{
					if (columnWidthArrs[i] != 0)
					{
						this.dgvSystemManage.Columns[i].Width = this.dgvSystemManage.Columns[i].Width + columnWidthArrs[i];
					}
				}
				this.isModify = true;
				return;
			}
			if (this.isModify && this.widths.Sum() > width)
			{
				for (int j = 0; j < this.widths.Count; j++)
				{
					this.dgvSystemManage.Columns[j].Width = this.widths[j];
				}
				this.isModify = false;
			}
		}

		private int[] GetColumnWidthArrs(int nums, int count)
		{
			int[] array = new int[count];
			int num = nums / count;
			int num2 = nums % count;
			if (nums < count)
			{
				for (int i = 1; i <= nums; i++)
				{
					array[i] = 1;
				}
			}
			if (nums == count)
			{
				for (int j = 0; j < count; j++)
				{
					array[j] = 1;
				}
			}
			if (nums > count)
			{
				for (int k = 0; k < count - num2; k++)
				{
					array[k] = num;
				}
				for (int l = count - num2; l < count; l++)
				{
					array[l] = num + 1;
				}
			}
			return array;
		}

		private Document doc;

		private List<string> vecWireType = new List<string>();

		private List<string> vecLayingMode = new List<string>();

		private List<string> vecPipeType = new List<string>();

		private List<string> vecLocalName = new List<string>();

		private List<string> vecSwitchType = new List<string>();

		private List<CSystemDataBlock> allPowerAndPanel = new List<CSystemDataBlock>();

		private List<CSystemDataBlock> powerList = new List<CSystemDataBlock>();

		private int rowsCount = 3;

		private List<string> powerGuid = new List<string>();

        //private ElectricSystemSettingData data = new ElectricSystemSettingData();

		private List<int> widths = new List<int>();

		private bool isModify;

		private int clickCount;

		private bool dgvValueIsNull = true;

		private int AddRowsCount;

		private string panelGuid = "";
	}
}
